#!/bin/bash
# 删除ftp用户
# auto donghai

while [ true ]; do

read -r -p "请输入你要删除的用户名：" username
dg=$(grep -n "$username" /etc/vsftpd/vsftpd_user.txt |awk -F ":" '{print $1}')
if [[ "$dg" != null && $(($dg % 2)) != 0 ]]; then
     break;
else
     echo "用户名不存在"
fi

done

lines=1

if [ ! -d /etc/vsftpd/vsftpd_user ]; then
rm -rf /etc/vsftpd/vsftpd_user
fi

touch /etc/vsftpd/vsftpd_user


for line in $(cat /etc/vsftpd/vsftpd_user.txt )
do
if [[ $lines -eq $dg  ||  $lines -eq  $(($dg + 1)) ]]; then
    ((lines++))
    continue
fi
cat <<EOF >>/etc/vsftpd/vsftpd_user
$line
EOF
((lines++))
done

rm -rf /etc/vsftpd/vsftpd_user.txt
mv /etc/vsftpd/vsftpd_user /etc/vsftpd/vsftpd_user.txt

db_load -T -t hash -f /etc/vsftpd/vsftpd_user.txt /etc/vsftpd/vsftpd_login.db

rm -rf /etc/vsftpd/user_conf/"$username"

if [ $? -eq 0 ]; then

     echo "删除成功！！"
else
     echo "删除失败！！"
fi

